<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_home extends CI_Controller {

	function index()
	{
		$data['error'] ='display: none;';
		$data['home']  ='active';
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{
			$data['title'] = 'Admin Home page';	
			$this->load->model('admin_model');
			$data['results'] = $this->admin_model->home_about();
			$data['message'] = $this->admin_model->list_message();	
			$data['content'] = 'admin/admin_home';
			$this->load->view('admin/template/template',$data);	
			
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}
	}
	
	function list_slider()
	{
		$data['error'] ='display: none;';
		$data['home']  ='active';
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{
				
			$data['title'] = 'Admin Slider';	
			$this->load->model('admin_model');
			$data['results'] = $this->admin_model->home_slider();	
			$data['message'] = $this->admin_model->list_message();
			$data['content'] = 'admin/admin_slider';
			$this->load->view('admin/template/template',$data);	
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}
	}
	
	function add_slider()
	{
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{	
		$data['error'] ='display: none;';
		$data['home']  ='active';
			if($this->session->userdata('email') || $this->session->userdata('logged'))
			{
				$data['title'] = 'Add Slider';	
				$data['content'] = 'admin/admin_slider_add';
				$this->load->view('admin/template/template_DT',$data);	
			}
			else 
			{
				$this->load->view('admin/signup',$data);
			}
		}
	}
	
	function update_upload(){
		if($this->session->userdata('email') || $this->session->userdata('logged')) 
		{
			if($this->uri->segment(3))
			{
				$this->load->model('admin_model');
				$data['message'] = $this->admin_model->list_message();
				$data['rows'] = $this->admin_model->list_update_slider($this->uri->segment(3));
				// variable transmis à la vue
				$data['id_slider'] = $data['rows']->id_slider;
				$data['fr_title'] = $data['rows']->fr_title;
				$data['fr_subtitle'] = $data['rows']->fr_subtitle;
				$data['fr_description'] = $data['rows']->fr_description;
				
				$data['en_title'] = $data['rows']->en_title;
				$data['en_subtitle'] = $data['rows']->en_subtitle;
				$data['en_description'] = $data['rows']->en_description;
						
				$data['photos'] = $data['rows']->photos;
				
				//condition upload
				$config['upload_path'] = './uploads/';
				$config['allowed_types'] = 'gif|png';
				$config['max_size'] = '3048';
				$config['encrypt_name'] = true;
				$this->load->library('upload',$config);

				if (!$this->upload->do_upload()) 
				{
					$this->lang->load('upload', $this->config->item('language'));
					$data['error_upload'] = $this->upload->display_errors();
					$data['title'] = 'Add Slider';
					$data['content'] = 'admin/admin_slider_update';
					$this->load->view('admin/template/template_DT',$data);
				}
				else 
				{
					$this->load->model('admin_model');
					$data['message'] = $this->admin_model->list_message();
					//-- supprimer image ancienne
					$donne['row'] = $this->admin_model->list_update_slider($this->uri->segment(3));
					echo $IMAGE = $donne['row']->photos;
					@unlink('uploads/'.$IMAGE);
					@unlink('uploads/thumbs/'.$IMAGE);
					//update 
					$image_data = $this->upload->data();
					$config = array(
						'image_library' => 'GD2', 
						'source_image' => $image_data['full_path'],
						'new_image' => './uploads/thumbs/',
						'create_thumb' => TRUE,
						'thumb_marker' => '',
						'maintain_ratio' => FALSE,
						'width'=> 80,
						'height' => 80
					);
					$this->load->library('image_lib',$config);
					$this->image_lib->resize();
					$image_name = $image_data['file_name'];
					$this->load->model('admin_model');
					$data['message'] = $this->admin_model->list_message();
					$data = array(
					'photos' => $image_name,
					'fr_title' => $this->input->post('title_fr'),
					'fr_subtitle' => $this->input->post('subtitle_fr'),
					'fr_description' => $this->input->post('description_fr'),
					'en_title' => $this->input->post('title_en'),
					'en_subtitle' => $this->input->post('subtitle_en'),
					'en_description' => $this->input->post('description_en'),
					);
					$this->admin_model->update_slider($this->uri->segment(3),$data);
					$data['success'] ='display: block;';
					redirect('admin_home/list_slider',$data);
					
				}
				
			}
			else 
			{
				redirect('admin_home/list_slider',$data);	
			}		
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}
	}
	
	function do_upload(){
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{
				$config['upload_path'] = './uploads/';
				$config['allowed_types'] = 'gif|png';
				$config['max_size'] = '3048';
				$config['encrypt_name'] = true;
				$this->load->library('upload',$config);

				if (!$this->upload->do_upload()) 
				{
					$this->lang->load('upload', $this->config->item('language'));
					$data['error_upload'] = $this->upload->display_errors();
					$data['title'] = 'Add Slider';
					$data['content'] = 'admin/admin_slider_add';
					$this->load->view('admin/template/template_DT',$data);
				} 
				else 
				{
					$image_data = $this->upload->data();
					$config = array(
						'image_library' => GD2, 
						'source_image' => $image_data['full_path'],
						'new_image' => './uploads/thumbs/',
						'create_thumb' => TRUE,
						'thumb_marker' => '',
						'maintain_ratio' => FALSE,
						'width'=> 80,
						'height' => 80
					);
					$this->load->library('image_lib',$config);
					$this->image_lib->resize();
					$image_name = $image_data['file_name'];
					$this->load->model('admin_model');
					$data['message'] = $this->admin_model->list_message();
					$data = array(
					'photos' => $image_name,
					'fr_title' => $this->input->post('title_fr'),
					'fr_subtitle' => $this->input->post('subtitle_fr'),
					'fr_description' => $this->input->post('description_fr'),
					'en_title' => $this->input->post('title_en'),
					'en_subtitle' => $this->input->post('subtitle_en'),
					'en_description' => $this->input->post('description_en'),
					);

					$this->admin_model->add_slider($data);
					$data['success'] ='display: block;';
					redirect('admin_home/list_slider',$data);
				}
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}	
	}

	function delete_slider()
	{
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{
			if($this->uri->segment(3))
			{
				
			$this->load->model('admin_model');
			$data['message'] = $this->admin_model->list_message();
			$data['row'] = $this->admin_model->get_slider($this->uri->segment(3));
			$IMAGE = $data['row']->photos;
			
			@unlink('uploads/'.$IMAGE);
			@unlink('uploads/thumbs/'.$IMAGE);	
			
			$this->load->model('crud_model');
			$this->crud_model->delete_message($this->uri->segment(3));
			
			$data['title'] = 'Admin Slider';
			$this->load->model('admin_model');
			$data['success'] ='display: block;';
			$data['results'] = $this->admin_model->home_slider();
			$data['message'] = $this->admin_model->list_message();
			$data['content'] = 'admin/admin_slider';
			$this->load->view('admin/template/template',$data);
			}
			else 
			{
			
			$data['error'] = 'display: block;';
			$data['title'] = 'Admin Slider';
			$this->load->model('admin_model');
			$data['results'] = $this->admin_model->home_slider();
			$data['message'] = $this->admin_model->list_message();
			$data['content'] = 'admin/admin_slider';
			$this->load->view('admin/template/template',$data);
			
			}
		}
		else {
			$this->load->view('admin/signup');
		}
	}

	function admin_slider_update()
	{
		$data['error'] ='display: none;';
		$data['home']  ='active';
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{
				
			$data['title'] = 'Update Slider';	
			$this->load->model('admin_model');
			if ($this->uri->segment(3)) 
			{
			$this->load->model('admin_model');
			$data['message'] = $this->admin_model->list_message();
			$data['rows'] = $this->admin_model->list_update_slider($this->uri->segment(3));
			// variable transmis à la vue
			$data['id_slider'] = $data['rows']->id_slider;
			$data['fr_title'] = $data['rows']->fr_title;
			$data['fr_subtitle'] = $data['rows']->fr_subtitle;
			$data['fr_description'] = $data['rows']->fr_description;
			
			$data['en_title'] = $data['rows']->en_title;
			$data['en_subtitle'] = $data['rows']->en_subtitle;
			$data['en_description'] = $data['rows']->en_description;
					
			$data['photos'] = $data['rows']->photos;	
			} 
			else 
			{
				redirect('admin_home/list_slider');
			}
			
			
			$data['content'] = 'admin/admin_slider_update';
			$this->load->view('admin/template/template',$data);	
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}
	}

	
	function update_about_fr()
	{
		if($this->session->userdata('email') || $this->session->userdata('logged')) 
		{
			if($this->uri->segment(3))
			{
				$this->load->model('admin_model');
				$data['message'] = $this->admin_model->list_message();
				$data['rows'] = $this->admin_model->list_update_about($this->uri->segment(3));
				// variable transmis à la vue
				$data['photos'] = $data['rows']->photos;
				
				$config['upload_path'] = './uploads/';
				$config['allowed_types'] = 'gif|png|jpg|jpeg';
				$config['max_size'] = '3048';
				$config['encrypt_name'] = true;
				$this->load->library('upload',$config);	
								
				if (!$this->upload->do_upload()) 
				{
					$this->lang->load('upload', $this->config->item('language'));
					$data['error_upload'] = $this->upload->display_errors();
					$data['title'] = 'Add Slider';
					$this->load->model('admin_model');
					$data['message'] = $this->admin_model->list_message();
					$data['results'] = $this->admin_model->home_about();	
					
					$data['content'] = 'admin/admin_home';
					$this->load->view('admin/template/template',$data);
				}
				else 
				{
					$IMAGE = $data['photos'];

					@unlink('uploads/'.$IMAGE);
					@unlink('uploads/thumbs/'.$IMAGE);
					
					$image_data = $this->upload->data();
					$config = array(
						'image_library' => 'GD2', 
						'source_image' => $image_data['full_path'],
						'new_image' => './uploads/thumbs/',
						'create_thumb' => TRUE,
						'thumb_marker' => '',
						'maintain_ratio' => FALSE,
						'width'=> 360,
						'height' => 240
					);
					
					$this->load->library('image_lib',$config);
					$this->image_lib->resize();
					$image_name = $image_data['file_name'];
					$this->load->model('admin_model');
					$data['message'] = $this->admin_model->list_message();
					$data = array(
					'photos' => $image_name,
					'fr_content' => $this->input->post('comment'),
					'fr_suite' => $this->input->post('second_comment'),
					);
					
					$this->load->model('crud_model');
					$this->crud_model->update_about_fr($this->uri->segment(3),$data);
					//-- supprimer image ancienne
					
					$this->load->model('admin_model');
					$data['message'] = $this->admin_model->list_message();
					$data['results'] = $this->admin_model->home_about();
					$data['content'] ='admin/admin_home';
					$data['update'] = 'display: block;';
					$this->load->view('admin/template/template',$data);
					
				}

			}
			else 
			{
				redirect('admin_home');	
			}
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}		
	}
	
	function update_about_en()
	{
		$this->form_validation->set_rules('comment_en', 'comment_en', 'trim|required|min_length[1]|xss_clean');
		$this->form_validation->set_rules('second_comment_en','Second_comment_en','trim|required|min_length[12]|xss_clean');
		if ($this->form_validation->run())
		{
			$data = array(
				'en_content' => $this->input->post('comment_en'),
				'en_suite' => $this->input->post('second_comment_en'),
				);
			$this->load->model('crud_model');	
			$this->crud_model->update_about_fr($this->uri->segment(3),$data);
						
			$this->load->model('admin_model');
			$data['message'] = $this->admin_model->list_message();
			$data['results'] = $this->admin_model->home_about();
			//$config['total_rows'] = $this->db->count_all('landing_user');
			$data['content'] ='admin/admin_home';
			//$data['members'] = 'active';
			//$data['count'] = $count;
			$data['update'] = 'display: block;';
			$this->load->view('admin/template/template',$data);
		}
	}
		
}		
?>